<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product extends CI_Controller {

    function index() {
        $data['site'] = 'site/product';
        $this->load->view('template', $data);
    }

    function catetablet() {
        $data['site'] = 'site/product';
        $this->load->view('template', $data);
    }

    function product_detail() {
        include_once 'loading_alway.php';
        $id = $this->uri->segment(3);
        
        $data['product'] = $this->product_model->getProductByID($id);
        $viewer=$data['product']->product_viewer;
        $this->product_model->updateViewer($id,$viewer+1);
        $data['product_default_image'] = $this->product_image_model->getImagesDefaultByProduct($id);
        $data['product_images'] = $this->product_image_model->getImagesByProduct($id);
        $data['site'] = 'site/product-detail';
        $this->load->view('template', $data);
    }

    function product_accessory() {
        $data['site'] = 'site/accessories';
        $this->load->view('template', $data);
    }

    function product_by_category() {
        include_once 'loading_alway.php';
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);
        if ($page <= 1)
            $page = 0;
        else
            $page = ($page - 1) * PAGE_SIZE;
        $total = $this->product_model->getProductByCategory_Count($id);
        $category_name = $this->category_model->getCategoryByID($id)->category_name;
        $data['site'] = 'site/product-by-category';

        $data['category_name'] = $category_name;
        $data['product_by_category'] = $this->product_model->getProductByCategory($id, $page);
        $link = 'product/product_by_category/' . $id;
        set_paging($link, $total, 4);
        $data['link'] = $total;
        $this->load->view('template', $data);
    }

    function old_product_by_category() {
        include_once 'loading_alway.php';
        $id = $this->uri->segment(3);
        $category_name = $this->category_model->getCategoryByID($id)->category_name;
        $data['site'] = 'site/product-by-category';

        $data['category_name'] = $category_name;
        $data['product_by_category'] = $this->product_model->getOldProductByCategory($id);
        $this->load->view('template', $data);
    }

    function old_product() {
        include_once 'loading_alway.php';
        $data['site'] = 'site/old-product';
        $data['old_products'] = $this->product_model->getOldProducts();
        $this->load->view('template', $data);
    }

    function search() {
        include_once 'loading_alway.php';
        $keyword = $this->input->get('txtsearch');
        if ($keyword == "Tên sản phẩm") {
            $keyword = "";
        }
        $category = $this->input->get('selcategory');
        $price_from = $this->input->get('selPriceFrom');
        $price_to = $this->input->get('selPriceTo');
        $category_strs = explode("-", $category);
        $data['keyword'] = $keyword;
        $data['accessories'] = null;
        $data['products'] = null;
        $data['price_from'] = $price_from;

        if ($category_strs[0] == "sp") {
            $data['product_result_title'] = $category_strs[2];
            $data['products'] = $this->product_model->search($keyword
                    , $category_strs[1], $price_from, $price_to);
        } else if ($category_strs[0] == "lk") {
            $data['accessory_result_title'] = $category_strs[2];
            $data['accessories'] = $this->accessory_model->search($keyword
                    , $category_strs[1], $price_from, $price_to);
        } else if ($category_strs[0] == "all") {
            $data['product_result_title'] = 'Điện thoại - Máy tính bảng';
            $data['accessory_result_title'] = 'Linh kiện';
            $data['products'] = $this->product_model->search($keyword
                    , $category_strs[1], $price_from, $price_to);
            $data['accessories'] = $this->accessory_model->search($keyword
                    , $category_strs[1], $price_from, $price_to);
        } else if ($category == "san-pham") {
            $data['product_result_title'] = 'Điện thoại - Máy tính bảng';
            $data['products'] = $this->product_model->search($keyword
                    , $category_strs[1], $price_from, $price_to);
        } else {
            $data['accessory_result_title'] = 'Linh kiện';
            $data['accessories'] = $this->accessory_model->search($keyword
                    , $category_strs[1], $price_from, $price_to);
        }
        $data['site'] = 'site/search';
        $this->load->view('template', $data);
    }

}